cbuffer SceneConstantBuffer : register(b0)
{
	matrix model;
	matrix view;
	matrix projection;
	float4 diffuse;
};

struct PSInput
{
	float4 position : SV_POSITION;
	float4 color : COLOR;
	float2 uv : TEXCOORD0;
	float3 worldNorm : TEXCOORD1;
	float3 worldPos : TEXCOORD2;
	float3 toEye : TEXCOORD3;
	float4 tangent : TEXCOORD4;
	float3 normal : TEXCOORD5;
};
Texture2D g_texture : register(t0);
SamplerState    g_sampler    : register(s0);


PSInput VSMain(float4 position : POSITION, float4 color : COLOR, float2 uv : TEXCOORD0, float3 normal : NORMAL)
{
	PSInput result=(PSInput)0;

	float4 pos = position;
	pos = mul(pos, model);
	pos = mul(pos, view);
	pos = mul(pos, projection);
	result.position = pos;
	result.color = diffuse;
	result.uv = uv;
	result.worldPos = pos.xyz / pos.w;
	result.worldNorm = normalize(mul(mul(normal.xyz, (float3x3)model), (float3x3)view));
	result.normal = normal;

	return result;
}

//
// lambert lighting function
//
float3 LambertLighting(
	float3 lightNormal,
	float3 surfaceNormal,
	float3 materialAmbient,
	float3 lightAmbient,
	float3 lightColor,
	float3 pixelColor
)
{
	// compute amount of contribution per light
	float diffuseAmount = saturate(dot(lightNormal, surfaceNormal));
	float3 diffuse = diffuseAmount * lightColor * pixelColor;

	// combine ambient with diffuse
	return saturate((materialAmbient * lightAmbient) + diffuse);
}

float4 CombineRGBWithAlpha(float3 rgb, float a)
{
	return float4(rgb.r, rgb.g, rgb.b, a);
}

float4 PSMain(PSInput input) : SV_TARGET
{
	//float3 diffuse = g_texture.Sample(g_sampler, input.uv).rgb;
	//return input.color;
	//return diffuse;
	//return g_texture.Sample(g_sampler, input.uv);
	float4 diffuse= g_texture.Sample(g_sampler, input.uv);

	float3 surfaceNormal = normalize(input.normal);
	float3 surfaceTangent = normalize(input.tangent.xyz);
	float3 worldNormal = input.worldNorm;

	float3x3 localToTangent = transpose(float3x3(surfaceTangent, cross(surfaceNormal, surfaceTangent) , surfaceNormal));
	float3x3 worldToTangent = mul((float3x3)model, localToTangent);

	//float3 tangentLightDir = normalize(mul(float3(0,0,1), model));
	float3 tangentLightDir = normalize(float3(0.5,1,1));

	float3 local1 = LambertLighting(tangentLightDir, worldNormal, float4(0.5, 0.5, 0.5, 1).rgb, float4(0.6, 0.6, 0.6, 1).rgb, float4(0.9, 0.9, 0.9,1).rgb, input.color.rgb);
	return CombineRGBWithAlpha(local1, input.color.a);

}